'''
@Descripttion: 
@version: 
@encoding: utf-8
@Author: qiurongcan
Date: 2025-10-10 19:30:27
LastEditTime: 2025-10-13 16:10:33
'''

# 图论，扩散算法


class Solution:

    def numIslands(self, grid):

        def dfs(grid, i, j):

            if not 0 <= i < len(grid) or not 0 <= j < len(grid[0]) or grid[i][j] == "0":
                return
            
            grid[i][j] = "0"

            dfs(grid, i + 1, j)
            dfs(grid, i - 1, j)
            dfs(grid, i, j + 1)
            dfs(grid, i, j - 1)


        count = 0
        for i in range(len(grid)):
            for j in range(len(grid[0])):

                if grid[i][j] == "1":
                    dfs(grid, i, j)
                    count += 1

        
        return count
            




